package com.example.service;

import com.example.common.Result;
import com.example.entity.ActivitySignup;
import com.example.entity.OrganizationActivity;
import com.example.mapper.ActivitySignupMapper;
import com.example.mapper.OrganizationActivityMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ActivitySignupService {
    @Resource
    private ActivitySignupMapper activitySignupMapper;
    @Resource
    private OrganizationActivityMapper activityMapper;

    @Transactional
    public Result signup(ActivitySignup signup) throws ParseException {
        // 获取活动信息
        OrganizationActivity activity = activityMapper.selectById(signup.getActivityId());
        if (activity == null) {
            return Result.error("活动不存在");
        }

        // 检查活动状态
        Date now = new Date();
        Date deadline = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(activity.getSignupDeadline());
        if (now.after(deadline)) {
            return Result.error("报名已截止");
        }

        // 检查人数限制
        int currentCount = activitySignupMapper.countValidSignups(signup.getActivityId());
        if (currentCount >= activity.getMaxParticipants()) {
            return Result.error("活动名额已满");
        }

        // 检查是否重复报名
        ActivitySignup existingSignup = activitySignupMapper.selectByActivityAndUser(
            signup.getActivityId(), signup.getUserId());
        if (existingSignup != null && existingSignup.getStatus() != 0) {
            return Result.error("您已报名此活动");
        }

        // 设置报名信息
        signup.setSignupTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
        signup.setStatus(1); // 已报名
        signup.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));

        // 保存报名记录
        activitySignupMapper.insert(signup);

        // 更新活动当前人数
        activityMapper.incrementParticipants(signup.getActivityId());

        return Result.success("报名成功");
    }


    public List<Map<String, Object>> getMyActivities(Long userId) {
        return activitySignupMapper.selectMyActivitiesWithDetail(userId);
    }

    @Transactional
    public Result cancelSignup(Long id, Long userId) {
        ActivitySignup signup = activitySignupMapper.selectById(id);
        if (signup == null || !signup.getUserId().equals(userId)) {
            return Result.error("无效的报名记录");
        }

        if (signup.getStatus() == 0) {
            return Result.error("该报名已取消");
        }

        // 更新报名状态
        signup.setStatus(0);
        activitySignupMapper.update(signup);

        // 更新活动当前人数
        activityMapper.decrementParticipants(signup.getActivityId());

        return Result.success("取消报名成功");
    }

    public PageInfo<ActivitySignup> selectMyPage(ActivitySignup activitySignup, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ActivitySignup> list = activitySignupMapper.selectAll(
                activitySignup.getUserId(),
                activitySignup.getStatus(),
                activitySignup.getTitle(),
                activitySignup.getActivityType()
        );
        return new PageInfo<>(list);
    }

}
